package com.shf.common
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.printing.FlexPrintJob;
	import mx.printing.FlexPrintJobScaleType;
	import mx.utils.StringUtil;
	
	public class ExpressPrint
	{
		public function ExpressPrint()
		{
			page.width = 23*28.346;
			page.height = 12.7*28.346;
		}
		
		public var expressArray:ArrayCollection;
		
		public var page:Object = new Object();
		
		public var configFiles:XML;
		
		public var printBackGroupImage:Boolean = false;
		
		public var backGroupImageBitMap:Bitmap;
		
		public function print(express:ArrayCollection = null,page:Object = null,configFiles:XML = null):void{
			if(express!=null)expressArray = express;
			if(page!=null)this.page = page;
			if(configFiles!=null)this.configFiles = configFiles;
			var printJob:FlexPrintJob;
			try
			{
				printJob=new FlexPrintJob();
				printJob.printAsBitmap = false;
			}
			catch(e:Error)
			{
				Alert.show("当前有打印作业在进行，无法开始新的打印，请稍候再试。");
			}
			if(page == null)page = this.page;
			if (printJob.start()){
				trace(printJob.pageHeight);
				trace(printJob.pageWidth);
				for each(var item:Object in expressArray){
					var target:Canvas = createPage(item,page)
					Application.application.addChild(target);
					try{
						trace("addpage");
						trace("	"+target.height);
						trace("	"+target.width);
//						printJob.addObject(target);
						printJob.addObject(target,FlexPrintJobScaleType.NONE);
					}catch(e:Error){
						trace(e.toString());
					}
					Application.application.removeChild(target);	
				}
				printJob.send();
			}
		}
		
		/**
		 * 创建打印页面
		 */ 
		private function createPage(item:Object,page:Object):Canvas{
			var express:Canvas = new Canvas();
			express.setStyle("backgroundColor","#FFFFFF");
			express.setStyle("backgroundAlpha",1);
			express.width = page.width;
			express.height = page.height;
			if(printBackGroupImage){
				if(backGroupImageBitMap)
					drawExpressBackGroup(express,backGroupImageBitMap,page.width,page.height);
				else
					Alert.show("打印时没有设置背景图片!");
			}
			updateCanvasChilds(configFiles,express,item);
			return express;
		}
		
		/**
		 * 更新打印模板值
		 * */
		public function updateCanvasChilds(configFiles:XML,bodyCanvas:Canvas,value:Object):void
		{
			bodyCanvas.removeAllChildren();
			var bodys:XMLList=configFiles.child("text");
			for each(var body:XML in bodys)
			{
				var footerText:Text = new Text();
				footerText.x=body.@x;
				footerText.y=body.@y;
				footerText["text"]=body.@text;
				footerText.name=body.@name;
				var data:Object = new Object();
				data.type = body.@type;
				footerText["data"] = data;
				if(body.@width!=null && StringUtil.trim(body.@width)!="")
					footerText.width=parseFloat(body.@width);
				if(body.@height!=null && StringUtil.trim(body.@height)!="")
					footerText.height=parseFloat(body.@height);
				footerText.setStyle("fontSize", body.@fontSize);
				footerText.setStyle("fontFamily", body.@fontFamily);
				if(body.@textAlign!=null && StringUtil.trim(body.@textAlign)!="") 
				footerText.setStyle("textAlign", body.@textAlign);
				footerText.doubleClickEnabled=true;
				footerText.htmlText=evalRealTextValue(body.@name, body.@text,value);
				if(body.@textAlign!=null && StringUtil.trim(body.@textAlign)!="") 
					footerText.setStyle("textAlign", body.@textAlign);
				bodyCanvas.addChild(footerText);
			}
		}
		
		public function evalRealTextValue(name:String, text:String,value:Object):String{
			if(name==null || StringUtil.trim(name)=="")return text;
			return value[name];
		}
		
		public function  drawExpressBackGroup(canvas:Canvas,bitmap:Bitmap,paperWidth:Number,paperHeight:Number):void{
		   	var bitmapData:BitmapData = new BitmapData(bitmap.width, bitmap.height);  
		   	var matrix:Matrix = new Matrix();
		   	matrix.scale(paperWidth/bitmap.width,paperHeight/bitmap.height);
	    	bitmapData.draw(bitmap,matrix,null,null,null,true);
	    	canvas.width = paperWidth;
	    	canvas.height = paperHeight;
	    	canvas.graphics.clear();  
	        canvas.graphics.beginBitmapFill(bitmapData);  
	        canvas.graphics.drawRect(0, 0, paperWidth, paperHeight);  
	        canvas.graphics.endFill();  
		}
		
	}
}